package com.ruoyi.mall.app.service.impl;


import com.ruoyi.mall.app.service.DtsAdService;
import com.ruoyi.mall.common.domain.DtsAd;
import com.ruoyi.mall.common.mapper.DtsAdMapper;
import com.ruoyi.mall.common.mapper.DtsArticleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;

@Service
public class DtsAdServiceImpl implements DtsAdService {

    @Autowired
    private DtsAdMapper dtsAdMapper;

//    @Autowired
//    private RedisCache redisCache;


    @Autowired
    public RedisTemplate redisTemplate;


    @PostConstruct
    public void init()
    {
        List<DtsAd>  list = dtsAdMapper.selectDtsAdList(null);
        redisTemplate.opsForValue().set("aaa", list);
    }


    @Override
    public List<DtsAd> selectAll() {
        List<DtsAd> list = (List<DtsAd>) redisTemplate.opsForValue().get("aaa");
        if (list != null) {
            return list;
        }
        // 缓存未命中，查数据库
        list = dtsAdMapper.selectDtsAdList(null);
        // 回写缓存（注意设置合理过期时间，避免内存溢出）
        redisTemplate.opsForValue().set("aaa", list);
        return list;
    }
}
